#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#include "modulea.h"
#include "moduleb.h"



int main() {
    // 从文件中加载学生和课程数据

     // 课程数据(class.txt)
    FILE* courseFile = fopen("class.txt", "r");
    if (courseFile == NULL) {
        printf("Failed to open class.txt file.\n");
    } else {
        while (fscanf(courseFile, "%d %s %d %d %d %d",
                      &courses[courseCount].id, courses[courseCount].name,
                      &courses[courseCount].credits, &courses[courseCount].hours,
                      &courses[courseCount].selectedCount, &courses[courseCount].maxCapacity) == 6) {
            courseCount++;
        }
        fclose(courseFile);
    }


    // 学生数据 (student.txt)
    FILE* studentFile = fopen("student.txt", "r");
    if (studentFile == NULL) {
        printf("Failed to open student.txt file.\n");
    } else {
        while (fscanf(studentFile, "%d %s {", &students[studentCount].id, students[studentCount].name) == 2) {
            // 获得学生选课列表
            char ch;
            int cid;
            int j = 0;
            while (fscanf(studentFile, "%d%c", &cid, &ch) == 2) {
                for (int i = 0; i < MAX_COURSES; i++) {
                    if (courses[i].id == cid) {
                        memcpy(&students[studentCount].seletctedCourse[j++], &courses[i], sizeof(Course)); //结构体赋值
                        coursePeople[cid] ++;
                    }
                }
                if (ch == '}') {
                    break;
                }
            }
            fscanf(studentFile, " %d %d", &students[studentCount].totalCredits, &students[studentCount].courseNum);
            studentCount++;
        }

        fclose(studentFile);
    }
    updateSelectedCourse(courses);
   

    // Menu and user interaction
    int choice;

    do {
        printf("\n========== Student and Course Management ==========\n");
        printf("1. Add student\n");
        printf("2. Modify student\n");
        printf("3. Delete student\n");
        printf("4. View student details\n");
        printf("5. Add course\n");
        printf("6. Delete course\n");
        printf("7. View course details\n");
        printf("8. Modify course\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                addStudent();
                break;
            case 2:
                modifyStudent();
                break;
            case 3:
                deleteStudent();
                break;
            case 4:
                viewStudentDetails();
                break;
            case 5:
                addCourse();
                break;
            case 6:
                deleteCourse();
                break;
            case 7:
                viewCourseDetails();
                break;
            case 8:
                modifyCourse();
                break;
            case 0:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please try again.\n");
                break;
        }
    } while (choice != 0);

    // 写文件

    // 写学生信息(student.txt)
    studentFile = fopen("student.txt", "w");
    if (studentFile == NULL) {
        printf("Failed to open student.txt file for writing.\n");
    } else {
        int i;
        for (i = 0; i < studentCount; i++) {
            fprintf(studentFile, "%d %s {",
                    students[i].id, students[i].name);
            int length = students[i].courseNum;
            for (int j = 0; j < length; j++) {
                fprintf(studentFile, "%d", students[i].seletctedCourse[j].id);
                if (j < length-1) {
                    fprintf(studentFile, ", ");
                }else {
                    fprintf(studentFile, "} ");
                }
                
            }
            fprintf(studentFile, "%d %d\n", students[i].totalCredits, students[i].courseNum);
            
        }
        fclose(studentFile);
    }

    // 写课程信息(class.txt)
    updateSelectedCourse(courses);
    courseFile = fopen("class.txt", "w");
    if (courseFile == NULL) {
        printf("Failed to open class.txt file for writing.\n");
    } else {
        int i;
        for (i = 0; i < courseCount; i++) {
            fprintf(courseFile, "%d %s %d %d %d %d\n",
                    courses[i].id, courses[i].name,
                    courses[i].credits, courses[i].hours,
                    courses[i].selectedCount, courses[i].maxCapacity);
        }
        fclose(courseFile);
    }

    return 0;
}
